﻿#include"string.h"
namespace hl
{
	typedef char* iterator;//.cpp文件再重命名一下
	typedef const char* const_iterator;
	//-------------------------------------------------------------------------------------------------------
	//iterator
    //第一个字符的位置
	iterator string::begin()//----在.cpp文件中
	{
		return _str;
	}
	//最后一个字符的下一个位置
	iterator string::end()
	{
		return _str + _size;
	}
	//const_iterator
	const_iterator string::begin() const
	{
		return _str;
	}
	const_iterator string::end() const
	{
		return _str + _size;
	}
	//-------------------------------------------------------------------------------------------------------
	//construct
	//用字符串去构造对象
	string::string(const char* str)
	{
		_size = strlen(str);
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}
	//交换两个string对象---直接交换底层数据
	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}
	//现代版本
	string::string(const string& s)
	{
		string tmp(s._str);
		swap(tmp);
	}
	string& string::operator=(const string& s)
	{
		if (this != &s)//处理自我赋值
		{
			string tmp(s);
			swap(tmp);
		}
		return *this;
	}
	string& string::operator=(string tmp)
	{
		swap(tmp);
		return *this;
	}
	//析构
	string::~string()
	{
		if (_str)//非空才去delete
		{
			delete[] _str;
			_size = _capacity = 0;
			_str = nullptr;
		}
	}
	//-------------------------------------------------------------------------------------------------------
	// access
	char& string::operator[](size_t index)
	{
		assert(index < _size);
		return _str[index];
	}
	const char& string::operator[](size_t index) const
	{
		assert(index < _size);
		return _str[index];
	}
	//-------------------------------------------------------------------------------------------------------
	// capacity
	size_t string::size() const
	{
		return _size;
	}
	size_t string::capacity() const
	{
		return _capacity;
	}
	bool string::empty() const
	{
		return _capacity == 0;
	}
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_capacity = n;
			_str = tmp;
			_str[n] = '\0';
		}
	}
	void string::resize(size_t n, char c)
	{
		if (n > _size)
		{
			if (n > _capacity)
			{
				reserve(n > 2 * _capacity ? n : 2 * _capacity);
				while (_size != n)
					_str[_size++] = c;
			}
			else
			{
				while (_size != n)
				{
					_str[_size++] = c;
				}
				_str[_size] = '\0';//_size==n位置时，插入 \0
			}
		}
		else
		{
			_size = n;
			_str[_size] = '\0';
		}
	}
	//-------------------------------------------------------------------------------------------------------
    // modify
	void string::push_back(char c)
	{
		if (_size == _capacity)//空间不足才去扩容
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		_str[_size++] = c;
		_str[_size] = '\0';
	}
	string& string::operator+=(char c)
	{
		push_back(c);
		return *this;
	}
	void string::append(const char* str)
	{
		size_t len = strlen(str);
		size_t n = _size + len;
		if (n > _capacity)
			reserve(n > 2 * _capacity ? n : 2 * _capacity);
		strcpy(_str + _size, str);
		_size = n;
	}
	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}
	// 在pos位置上插入字符c/字符串str，并返回该字符的位置
	string& string::insert(size_t pos, char c)
	{
		assert(pos <= _size && pos >= 0);
		if (_size == _capacity)
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			--end;
		}
		_str[pos] = c;
		++_size;
		return *this;
	}
	string& string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size && pos >= 0);
		size_t len = strlen(str);
		size_t end = _size + len;
		if (end > _capacity)
			reserve(end > 2 * _capacity ? end : 2 * _capacity);
		while (end > pos + len - 1)
		{
			_str[end] = _str[end - len];
			--end;
		}
		size_t j = 0;
		for (size_t i = pos; i < pos + len; i++)
		{
			_str[i] = str[j++];
		}
		_size += len;
		return *this;
	}
	// 删除从pos位置(包含pos)开始后总共len个字符数，并返回该元素的下一个位置
	string& string::erase(size_t pos, size_t len)
	{
		assert(pos < _size && pos >= 0);
		if (len >= _size - pos)
		{
			_size = pos;
			_str[_size] = '\0';
		}
		else 
		{
			size_t end = pos + len;
			size_t i = pos;
			while (end <= _size)
			{
				_str[i++] = _str[end++];
			}
			_size -= len;
		}
		return *this;
	}
	void string::clear()
	{
		_size = 0;
		_str[0] = '\0';
	}
	
	//返回C格式的字符串
	const char* string::c_str() const
	{
		return _str;
	}
	//-------------------------------------------------------------------------------------------------------
    // find
    // 返回c在string中第一次出现的位置
	size_t string::find(char c, size_t pos) const
	{
		assert(pos < _size && pos >= 0);
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == c)
				return i;
		}
		return npos;
	}
	// 返回子串s在string中第一次出现的位置
	size_t string::find(const char* s, size_t pos) const
	{
		char* tmp = strstr(_str, s);//在_str中找子串s
		if (tmp == nullptr)//没有匹配的字串
			return npos;
		else
			return tmp - _str;
	}
	size_t string::find(const string& str, size_t pos) const
	{
		assert(pos < _size && pos >= 0);
		char* tmp = strstr(_str, str._str);
		if (tmp == nullptr)
			return npos;
		else
			return tmp - _str;
	}
	// 从pos位置开始(包括pos位置)，总共截取len个字符
	string string::substr(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len > _size - pos)
			len = _size - pos;
		string sub;
		sub.reserve(len);
		for (size_t i = pos; i < pos + len; i++)
			sub += _str[i];
		return sub;
	}
	//-------------------------------------------------------------------------------------------------------
	//relational operators---为了方便这里把关系运算符重载成成员函数，但库里是全局函数
	bool string::operator<(const string& s)
	{
		return strcmp(_str, s._str) < 0;
	}
	bool string::operator<=(const string& s)
	{
		return strcmp(_str, s._str) < 0 || strcmp(_str, s._str) == 0;
	}
	bool string::operator==(const string& s)
	{
		return strcmp(_str, s._str) == 0;
	}
	bool string::operator>(const string& s)
	{
		return !(*this <= s);
	}
	bool string::operator>=(const string& s)
	{
		return !(*this < s);
	}
	bool string::operator!=(const string& s)
	{
		return !(*this == s);
	}
	//-------------------------------------------------------------------------------------------------------
	//iostream
	ostream& operator<<(ostream& _cout, const string& s)
	{
		for (auto c : s)
		{
			_cout << c;
		}
		return _cout;
	}
	istream& operator>>(istream& _cin, string& s)
	{
		s.clear();
		const int N = 256;
		char buff[N];
		char ch;
		ch = _cin.get();
		int i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = _cin.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}
		return _cin;
	}
}